<?php
class MoncdsController extends AppController {

	var $name = 'Moncds';
	var $uses = array('Moncd','Ctdt','Mon','Khoa','Nganh','Nhomhp','Monthamgianhom');

	function index() {
		$this->Moncd->recursive = 0;
		$this->set('moncds', $this->paginate());
		
		
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid moncd', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('moncd', $this->Moncd->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Moncd->create();
			if ($this->Moncd->save($this->data)) {
				$this->Session->setFlash(__('Lưu thành công!', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Không thể lưu dữ liệu. Kiểm tra lại!', true));
			}
		}
		$ctdts = $this->Moncd->Ctdt->find('list');
		$mons = $this->Moncd->Mon->find('list');
		$this->set(compact('ctdts', 'mons'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid moncd', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Moncd->save($this->data)) {
				$this->Session->setFlash(__('Lưu thành công! ', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Không thể lưu dữ liệu. Kiểm tra lại!', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Moncd->read(null, $id);
		}
		$ctdts = $this->Moncd->Ctdt->find('list');
		$mons = $this->Moncd->Mon->find('list');
		$this->set(compact('ctdts', 'mons'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for moncd', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Moncd->delete($id)) {
			$this->Session->setFlash(__('Moncd deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Moncd was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
	
	function capnhatnganh(){
		$this->layout='ajax';
		
		$khoa = $this->data['Ctdt']['khoak']; 
		$nganhs = $this->Nganh->find('list',array('conditions'=>array('khoa_id'=>$khoa),'fields'=>array('tenNganh')));
		$this->set(compact('nganhs'));
	}
	
	function viewmoncd($khoa = null, $nganh = null, $khoahoc = null)
	{
		$this->set(compact('khoa','nganh','khoahoc'));
		//$nhom =array();
		if($khoa !==null)
		{
			if($nganh==0)
			{
				if($khoa!=0)
				{
					$nganhs = $this->Nganh->find('list',array('conditions'=>array('khoa_id'=>$khoa)));
					$nganh = array_keys($nganhs);
				}
				else
				{
					$nganhs = $this->Nganh->find('list');
					$nganh = array_keys($nganhs);
				}
			}
			if($khoahoc!=0)
					$this->paginate = array('conditions'=>array('nganh_id'=>$nganh,'Khoa'=>$khoahoc,'limit'=>1));
			else
					$this->paginate = array('conditions'=>array('nganh_id'=>$nganh),'limit'=>1);
		}
		$this->Moncd->recursive = 0;
		$this->set('moncds', $this->paginate());
		$khoas = $this->Khoa->find('list',array('fields'=>array('tenKhoa')));
		$nganhs = $this->Nganh->find('list',array('fields'=>array('tenNganh')));
		$khoahocs = $this->Ctdt->find('list',array('fields'=>array('Khoa','Khoa'),'order'=>array('Khoa DESC'),'group'=>array('Khoa')));
		$mons = $this->Mon->find('list',array('conditions'=>array($khoahoc,$nganh),'fields'=>array('mon')));
		$this->set(compact('khoas','nganhs','khoahocs','mons'));
	}
	
	function capnhatmoncd(){
		
	$this->layout = 'ajax';
	$nhoms=$this->Nhomhp->find('list',array('recursive'=>2,'fields'=>array('id'),'conditions'=>array('Ctdt.Khoa'=>$this->data['Ctdt']['khoahoc'],'Ctdt.nganh_id'=>$this->data['Ctdt']['nganh'])));
	$mons=$this->Monthamgianhom->find('all',array('conditions'=>array('Monthamgianhom.nhomhp_id'=>$nhoms)));
	$ctdt=null;
	if(isset($mons))
		{
			foreach ($mons as $monthamgianhom)
			{
					$monctdts[$monthamgianhom['Monthamgianhom']['id']]=$monthamgianhom['Mon']['mon'];
					$ctdt=$monthamgianhom['Nhomhp']['ctdt_id'];
			}
		}
	$newmons=$this->Mon->find('list',array('fields'=>'mon'));
	$this->set(compact('monctdts','newmons','ctdt'));
	}
		
	function capnhatnganhcd()
	{
		$this->layout='ajax';
		$khoa = $this->data['Ctdt']['khoa']; 
		if(!empty($khoa))
			$nganhs = $this->Nganh->find('list',array('conditions'=>array('khoa_id'=>$khoa),'fields'=>array('tenNganh')));
		else
			$nganhs = $this->Nganh->find('list',array('fields'=>array('tenNganh')));
		
		$this->set(compact('nganhs'));
	}
	
	function luumoncd(){
		//debug($this->data);
		foreach ($this->data['Moncd']['moncd']as $key)
		{
			$record=array();
			$record['Moncd']['monthamgianhom_id']=$key;
			$record['Moncd']['mon_id']=$this->data['Mon']['tenMon'];
			$record['Moncd']['ctdt_id']=$this->data['ctdt']; //////// sai ten thuoc tinh trong bang bd la ctdt da sua ctdt_id
			$this->Moncd->create();
			$this->Moncd->save($record);
			
			//debug($record);
				
		}
		//$this->redirect(array('action'=>'viewmoncd'));
	}

	function xemmoncd()
	{
		$khoas = $this->Khoa->find('list',array('fields'=>array('tenKhoa')));
		$nganhs = $this->Nganh->find('list',array('fields'=>array('tenNganh')));
		$khoahocs = $this->Ctdt->find('list',array('fields'=>array('Khoa','Khoa'),'order'=>array('Khoa DESC'),'group'=>array('Khoa')));
		$this->set(compact('khoas','nganhs','khoahocs','mons'));
	}
	
	function updatexemcd()
	{
		$this->layout = 'ajax';
		$nhoms=$this->Nhomhp->find('first',array('recursive'=>2,'fields'=>array('ctdt_id'),'conditions'=>array('Ctdt.Khoa'=>$this->data['Ctdt']['khoahoc'],'Ctdt.nganh_id'=>$this->data['Ctdt']['nganh'])));
		$ctdt=null;
		if(isset($nhoms['Nhomhp']['ctdt_id']))
			$ctdt=$nhoms['Nhomhp']['ctdt_id'];
		
		$mons=$this->Moncd->find('all',array('conditions'=>array('Moncd.ctdt_id'=>$ctdt)));
		if(isset($mons))
		{
				foreach ($mons as $mondacd)
				{
						$mondscds[$mondacd['Mon']['id']]=$mondacd['Mon']['mon'];
				}
		}
	///$mondacds = $this->Moncd->find('all',array('recursive'=>0,'conditions'=>array('ctdt_id'=>$ctdt),'fields'=>array('Mon.id','Mon.tenMon'))); 
		$this->set(compact('mondscds','ctdt'));
	}

	function updatecdmoi()
	{
		$this->layout = 'ajax';
		$moncu = $this->data['Ctdt']['listctdtcu'];
		$ctdt = $this->data['Ctdt']['ctdt'];
		$mons=$this->Moncd->find('all',array('recursive'=>2,'conditions'=>array('Moncd.ctdt_id'=>$ctdt,'Moncd.mon_id'=>$moncu)));

		if(isset($mons))
		{
				foreach ($mons as $mondacd)
				{
						$mondscds[$mondacd['Monthamgianhom']['Mon']['id']]=$mondacd['Monthamgianhom']['Mon']['mon'];
				}
		}
		$this->set(compact('mondscds'));
	}
}
?>
